package models

import "gorm.io/gorm"

type Video struct {
	gorm.Model
	Title        string  `gorm:"type:varchar(20);index;not null;comment:视频标题"`
	Briefly      string  `gorm:"type:varchar(30);comment:简介"`
	TypesId      int64   `gorm:"type:int;comment:分类ID 1-电影 2-电视剧 3-综艺"`
	Ads          int64   `gorm:"type:int;comment:地区ID 1-内地 2-欧美 3-日韩"`
	Channel      int64   `gorm:"type:int;comment:频道ID 1-喜剧 2-动作 3-爱情"`
	CoverUrl     string  `gorm:"type:varchar(200);comment:封面地址"`
	PlayUrl      string  `gorm:"type:varchar(200);comment:文件地址"`
	CheckStatus  int64   `gorm:"type:int;default:0;comment:审核状态0-待审核 1-通过 2-驳回"`
	CheckReason  string  `gorm:"type:varchar(20);comment:审核备注"`
	Likes        int64   `gorm:"type:int;default:0;comment:点赞量"`
	Collect      int64   `gorm:"type:int;default:0;comment:收藏量"`
	PlayCount    int64   `gorm:"type:int;default:0;comment:播放量"`
	CommentCount int64   `gorm:"type:int;default:0;comment:评论量"`
	Hot          int64   `gorm:"type:int;default:0;comment:热度"`
	LeadActor    string  //主演
	Director     string  //导员
	Score        float64 //评分
}

func (v *Video) CreateVideo(db *gorm.DB) error {
	return db.Create(&v).Error
}

func (v *Video) GetVideoById(db *gorm.DB, id int64) error {
	return db.Where("id=?", id).Limit(1).Find(&v).Error
}

// 审核状态修改
func (v *Video) UpdateCheckStatus(db *gorm.DB, id, status int64) error {
	return db.Model(&v).Where("id=?", id).Update("check_status", status).Error
}

func (v *Video) UpdateCheckReason(db *gorm.DB, id int64, CheckReason string) error {
	return db.Model(&v).Where("id=?", id).Update("check_reason", CheckReason).Error
}

func (v *Video) HotTotal(db *gorm.DB, id int64) (hot int64, err error) {
	db.Where("id=?", id).Limit(1).Find(&v)

	hot = int64(float64(v.PlayCount)*0.6 + float64(v.Likes)*0.2 +
		float64(v.Collect)*0.1 + float64(v.CommentCount)*0.1)

	return hot, err
}

func (v *Video) VideoList(db *gorm.DB) (listVideos []Video, err error) {
	query := db.Debug()

	err = query.Order("hot desc").Find(&listVideos).Error

	return listVideos, err
}

type VideoInfo struct {
	gorm.Model
	VideoId   int64
	LeadActor string //主演
	Director  string //导员
}

func (vi *VideoInfo) GetInfoById(db *gorm.DB, videoId int64) error {
	return db.Where("videoId=?", videoId).Limit(1).Find(&vi).Error
}

// 电视剧表
type Episode struct {
	gorm.Model
	Title     string `gorm:"type:varchar(20);not null;comment:视频标题"`
	AuthorId  int64  `gorm:"type:int;comment:制片方ID"`
	TypesId   int64  `gorm:"type:int;comment:分类ID 1-电影 2-电视剧"`
	EpisodeNo int64  `gorm:"type:int;default:0;comment:剧集数"`
	PlayCount int64  `gorm:"type:int;default:0;comment:播放量"`
	Long      string `gorm:"type:varchar(20);comment:时长"`
}

func (e *Episode) CreateEpisode(db *gorm.DB) error {
	return db.Create(&e).Error
}

func (e *Episode) GetEpsById(db *gorm.DB, id int64) error {
	return db.Where("id=?", id).Limit(1).Find(&e).Error
}

// 更新视频播放数
func (e *Episode) UpdateEpisodePlay(db *gorm.DB) error {
	return db.Model(&e).
		Where("id =?", e.ID).
		Update("play_count", gorm.Expr("play_count + ?", 1)).
		Error
}

func (e *Episode) EpisodeList(db *gorm.DB) (listEpisode []Episode, err error) {
	err = db.Find(&listEpisode).Error
	return listEpisode, err
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
